# -*- coding: utf-8 -*-
#
# author: Tobiasz Siemiński
# creation date: 10.05.2011


from PyQt4 import QtGui, QtCore, Qt
import algorithm as alg
import ioFunctions as iof
import datetime

# ==== STAŁE
polishDays = [u"Poniedzialek", "Wtorek", u"Sroda", "Czwartek", u"Piatek", "Sobota", "Niedziela"]
# ====

class QTeamGenerationResultWidget(QtGui.QWidget):
  
    def __init__(self, place, date, time, playersList = []):
        super(QTeamGenerationResultWidget, self).__init__()

	self.playersList = playersList #otrzymany slownik graczy - nickow
	self.place = place # miejsce zdarzenia
	self.date = date # data zdarzenia
	self.time = time # czas zdarzenia


	# utworz date na podstawie stringa ktory dostales
	tab = self.date.split("-")
	if len(tab) > 2:
		self.mydate = datetime.date(int(tab[0]), int(tab[1]), int(tab[2]))
		self.dayOfEvent = polishDays[self.mydate.weekday()]
		

	#CENTER WINDOW
	screen = QtGui.QDesktopWidget().screenGeometry()
	size = self.geometry()
	self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)

	#INIT UI
	self.setWindowTitle(u'Wygenerowane składy')
        self.initUI()


    # stworz wszystkie elementy i je dodaj
    def initUI(self):

	# rozdziel sklady gdy zostali przekazani zawodnicy
	if (len(self.playersList) > 0):
		self.team1, self.team2 = alg.generateTeams(self.playersList)
	else:
		self.team1 = {}
		self.team2 = {}

        self.team1Label = QtGui.QLabel(u"Drużyna 1")
	self.team2Label = QtGui.QLabel(u"Drużyna 2")
	
	# LISTA 1 druzyna
	self.team1PlayersList = QtGui.QListWidget()
	self.playersLabelsList = self.team1.keys()
	self.playersLabelsList.sort()
	self.team1PlayersList.addItems(QtCore.QStringList(self.playersLabelsList))

	# LISTA 2 druzyna
	self.team2PlayersList = QtGui.QListWidget()
	self.playersLabelsList2 = self.team2.keys()
	self.playersLabelsList2.sort()
	self.team2PlayersList.addItems(QtCore.QStringList(self.playersLabelsList2))

	# LABELE
	self.comLabel = QtGui.QLabel("Opanowanie:")
	self.pfLabel = QtGui.QLabel(u"Sprawność fizyczna:")
	self.techLabel = QtGui.QLabel("Technika:")
	self.engLabel = QtGui.QLabel(u"Zaangażowanie:")
	self.sumLabel = QtGui.QLabel("SUMA:")

	self.com = 0
	self.eng = 0
	self.tech = 0
	self.pf = 0
	for player in self.team1.values():
		self.com += player.composure
		self.eng += player.engagement
		self.tech += player.technique
		self.pf += player.physicalFitness
	self.sum1 = self.com+self.eng+self.tech+self.pf

	self.com2 = 0
	self.eng2 = 0
	self.tech2 = 0
	self.pf2 = 0
	for player in self.team2.values():
		self.com2 += player.composure
		self.eng2 += player.engagement
		self.tech2 += player.technique
		self.pf2 += player.physicalFitness
	self.sum2 = self.com2+self.eng2+self.tech2+self.pf2

	self.com1Label = QtGui.QLabel(str(self.com))
	self.eng1Label = QtGui.QLabel(str(self.eng))
	self.tech1Label = QtGui.QLabel(str(self.tech))
	self.pf1Label = QtGui.QLabel(str(self.pf))
	self.sum1Label = QtGui.QLabel(str(self.sum1))

	self.com2Label = QtGui.QLabel(str(self.com2))
	self.eng2Label = QtGui.QLabel(str(self.eng2))
	self.tech2Label = QtGui.QLabel(str(self.tech2))
	self.pf2Label = QtGui.QLabel(str(self.pf2))
	self.sum2Label = QtGui.QLabel(str(self.sum2))

 	# TEKST NA FORUM
	forForumLabel = QtGui.QLabel("Tekst do wklejenia na forum:")

	self.forForumText = QtGui.QTextEdit()	
	self.generateForumText(self.place, self.date, self.time, self.team1, self.team2)

	# PRZYCISKI ZAPISANIA
	forForumButton = QtGui.QPushButton("Zapisz tekst na forum")
	self.connect(forForumButton, Qt.SIGNAL("clicked()"), self.saveForumTextToFile)

	forNothingButton = QtGui.QPushButton(u"Zapisz zwykły tekst")
	self.connect(forNothingButton, Qt.SIGNAL("clicked()"), self.saveNormalTextToFile)

	closeButton = QtGui.QPushButton("OK")
	self.connect(closeButton, QtCore.SIGNAL('clicked()'), self.closeWindow)
	
	# ======== GRID
	grid = QtGui.QGridLayout()
	grid.setSpacing(10)

	grid.addWidget(self.team1Label, 0, 0)
	grid.addWidget(self.team2Label, 0, 2)

	grid.addWidget(self.team1PlayersList, 1, 0, 6, 2)
	grid.addWidget(self.team2PlayersList, 1, 2, 6, 2)
	
	
	grid.addWidget(self.comLabel, 7, 0)
	grid.addWidget(self.pfLabel, 8, 0)
	grid.addWidget(self.techLabel, 9, 0)
	grid.addWidget(self.engLabel, 10, 0)
	grid.addWidget(self.sumLabel, 11, 0)

	grid.addWidget(self.com1Label, 7, 1)
	grid.addWidget(self.pf1Label, 8, 1)
	grid.addWidget(self.tech1Label, 9, 1)
	grid.addWidget(self.eng1Label, 10, 1)
	grid.addWidget(self.sum1Label, 11, 1)

	grid.addWidget(self.com2Label, 7, 3)
	grid.addWidget(self.pf2Label, 8, 3)
	grid.addWidget(self.tech2Label, 9, 3)
	grid.addWidget(self.eng2Label, 10, 3)
	grid.addWidget(self.sum2Label, 11, 3)


	grid.addWidget(forForumLabel, 12, 0, 1, 3)
	grid.addWidget(self.forForumText, 13, 0, 4, 4)

	grid.addWidget(forNothingButton, 17, 0)
	grid.addWidget(forForumButton, 17, 1)
	grid.addWidget(closeButton, 17, 3)

	self.setLayout(grid)
       
    def closeWindow(self):
	self.hide()

    #ZAPISZ NORMALNY TEKST DO PLIKU
    def saveNormalTextToFile(self):

	filename = QtGui.QFileDialog.getSaveFileName(self, 'Zapisz plik', "")
	if filename == "":
		return

	#stworz naglowek
	if len(self.date.split("-")) > 2:
		roznica = self.mydate.day - datetime.date.today().day
		if roznica == 1:
			header = u"GRAMY W PIŁKĘ W DNIU"+self.date+"("+self.dayOfEvent+" JUŻ JUTRO !!!) O GODZINIE "+self.time+"\nMIEJSCE: "+self.place+"\n"
		else:
			header = u"GRAMY W PIŁKĘ W DNIU"+self.date+"("+self.dayOfEvent+") O GODZINIE "+self.time+"\nMIEJSCE: "+self.place+"\n"
	else:
		header = u"GRAMY W PIŁKĘ W DNIU"+self.date+" O GODZINIE "+self.time+"\nMIEJSCE: "+self.place+"\n"
	
	#stworz zawartosc - info o druzynach
	content = u"DRUŻYNA 1:\n"
	for player in self.team1:
		content = content + player+"\n"

	content = content+ u"DRUŻYNA 2:\n"
	for player in self.team2:
		content = content +player+"\n"
	content = content + "SERDECZNIE ZAPRASZAM"
	iof.saveTextToFile(filename, str(header)+str(content))

    #ZAPISZ TEKST DLA FORUM DO PLIKU
    def saveForumTextToFile(self):
	filename = QtGui.QFileDialog.getSaveFileName(self, 'Zapisz plik', "")
	if filename == "":
		return
	iof.saveTextToFile(filename, str(self.forForumText.toPlainText()))


    #wygeneruj tekst na forum do wklejenia od razu gotowe :)
    def generateForumText(self, place, date, time, team1, team2):
	
	#stworz naglowek
	if len(self.date.split("-")) > 2:
		roznica = self.mydate.day - datetime.date.today().day
		if roznica == 1:
			header = "[b][size=21]GRAMY W FOOTBALL W DNIU [color=red]"+date+"("+self.dayOfEvent+" JUZ JUTRO !!!)[/color] O GODZINIE [color=red]"+time+"[/color][/size][/b]\n[b][size=18]MIEJSCE: [color=red]"+place+"[/color][/size][/b]\n"
		else:
			header = "[b][size=21]GRAMY W FOOTBALL W DNIU [color=red]"+date+"("+self.dayOfEvent+")[/color] O GODZINIE [color=red]"+time+"[/color][/size][/b]\n[b][size=18]MIEJSCE: [color=red]"+place+"[/color][/size][/b]\n"
	else:
		header = "[b][size=21]GRAMY W FOOTBALL W DNIU [color=red]"+date+"[/color] O GODZINIE [color=red]"+time+"[/color][/size][/b]\n[b][size=18]MIEJSCE: [color=red]"+place+"[/color][/size][/b]\n"
	
	#stworz zawartosc - info o druzynach
	content = u"[b][size=14]DRUZYNA 1:[/size][/b]\n"
	for player in team1:
		content = content +player+"\n"
	content +=u"[b][size=14]STATYSTYKI 1:[/size][/b]\nOpanowanie: "+str(self.com)+u"\nSprawnosc fizyczna: "+str(self.pf)+"\nTechnika: "+str(self.tech)+u"\nZaangazowanie: "+str(self.eng)+"\n[b]SUMA: "+str(self.sum1)+"[/b]\n\n"

	content = content+ u"[b][size=14]DRUZYNA 2:[/size][/b]\n"
	for player in team2:
		content = content +player+"\n"
	content +=u"[b][size=14]STATYSTYKI 2:[/size][/b]\nOpanowanie: "+str(self.com2)+u"\nSprawnosc fizyczna: "+str(self.pf2)+"\nTechnika: "+str(self.tech2)+u"\nZaangazowanie: "+str(self.eng2)+"\n[b]SUMA: "+str(self.sum2)+"[/b]\n\n"
	content = content + "SERDECZNIE ZAPRASZAM"
	self.forForumText.setText(unicode(str(header))+str(unicode(content)))








